<template>
  <div>
    <el-container>
      <el-header class="header">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item>
            <el-button type="primary" @click="add">添加品牌</el-button>
          </el-form-item>
        </el-form>
      </el-header>
      <el-main>
        <el-table
          ref="multipleTable"
          :data="brandData"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column label="logo">
            <template slot-scope="props">
              <el-image
                style="width: 80px; height: 80px"
                :src="props.row.logo"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="品牌名称" prop="name"></el-table-column>
          <el-table-column
            label="官网"
            prop="officialWebsite"
          ></el-table-column>
          <el-table-column label="类别" prop="typeName"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="keyword.currentPage"
          :page-size="keyword.pageSize"
          class="page"
          big
          :page-sizes="[1, 2, 3, 4, 5]"
          layout="total,sizes,prev, pager, next"
          :total="total"
        ></el-pagination>
      </el-main>
      <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
        <el-form :model="brand" ref="brandForm" label-width="100px">
          <el-form-item label="品牌logo：" prop="name">
            <input type="file" id="file" />
          </el-form-item>
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="brand.name"></el-input>
          </el-form-item>
          <el-form-item label="品牌官网" prop="officialWebsite">
            <el-input v-model="brand.officialWebsite"></el-input>
          </el-form-item>
          <el-form-item label="类别：" prop="typeId" label-width="100px">
            <el-cascader
              v-model="brand.typeId"
              :options="typeOptions"
              :props="{
                expandTrigger: 'hover',
                value: 'id',
                label: 'name',
                checkStrictly: true,
              }"
              clearable
              style="margin-left: -50px; width: 250px"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </span>
      </el-dialog>
    </el-container>
  </div>
</template>

<script>
import { getBrands, remove, insert, update, setLogo } from "@/api/brand.js";
import { getTypeList } from "@/api/type.js";
export default {
  name: "",
  data() {
    return {
      brand: {},
      title: "添加",
      keyword: { currentPage: 1, pageSize: 4 },
      total: 0,
      dialogVisible: false,
      brandData: [],
      typeOptions: [],
    };
  },
  created() {
    this.loadBrands();
    this.loadTypes();
  },
  methods: {
    cancel() {
      this.dialogVisible = false;
      this.$refs.brandForm.resetFields();
    },

    loadBrands(val) {
      if (val) {
        this.keyword.currentPage = 1;
      }
      getBrands(this.keyword).then((res) => {
        if (res.data.code === "0000") {
          this.brandData = res.data.data.records;
          this.total = Number.parseInt(res.data.data.total);
        }
      });
    },
    loadTypes() {
      getTypeList().then((res) => {
        if (res.data.code === "0000") {
          this.typeOptions = res.data.data;
        }
      });
    },
    handleSizeChange(val) {
      this.keyword.pageSize = val;
      this.loadBrands();
    },
    handleCurrentChange(val) {
      this.keyword.currentPage = val;
      this.loadBrands();
    },
    add() {
      (this.brand = {}), (this.title = "添加");
      this.dialogVisible = true;
    },
    handleEdit(index, row) {
      this.title = "编辑";
      this.dialogVisible = true;
      this.brand = row;
      console.log(index, row);
    },
    handleDelete(index, row) {
      this.$confirm("确认要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(row.id);
          remove(row.id).then((res) => {
            if (res.data.code == "0000") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.loadBrands();
            }
            console.log(res.data.code);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    save() {
      if (this.title == "添加") {
        var oFiles = document.getElementById("file").files;
        console.log(oFiles[0]);
        var params = new FormData();
        params.append("file", oFiles[0]);
        setLogo(params).then((res) => {
          if (res.data.code == "0000") {
            this.brand.logo = res.data.data;
            this.brand.typeId = this.brand.typeId[this.brand.typeId.length - 1];
            console.log(this.brand);
            insert(this.brand).then((res) => {
              if (res.data.code === "0000") {
                this.$message({
                  type: "info",
                  message: res.data.msg,
                });
                // this.$emit("show", this.dialogVisible);
                this.dialogVisible = false;
                this.loadBrands();
                this.$refs.brandForm.resetFields();
              }
            });
          } else {
            console.log("出错了");
          }
        });
      } else if (this.title == "编辑") {
        var oFiles1 = document.getElementById("file").files;
        console.log(oFiles1[0]);
        var params1 = new FormData();
        params1.append("file", oFiles1[0]);
        setLogo(params1).then((res) => {
          if (res.data.code == "0000") {
            this.brand.logo = res.data.data;
            this.brand.typeId = this.brand.typeId[this.brand.typeId.length - 1];
            update(this.brand).then((res) => {
              if (res.data.code === "0000") {
                this.$message({
                  type: "success",
                  message: res.data.msg,
                });
                // this.$emit("show", this.dialogVisible);
                this.dialogVisible = false;
                this.loadBrands();
                this.$refs.brandForm.resetFields();
              }
            });
          } else {
            console.log("出错了");
          }
        });
      }
    },
  },
};
</script>

<style>
.header {
  text-align: left;
}
</style>